package com.lianbang.staff.module.shop

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.ubest.mvvmcore.base.BaseFragment
import cn.ubest.mvvmcore.base.BaseListFragment
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.chad.library.adapter.base.module.LoadMoreModule
import com.google.android.material.appbar.AppBarLayout
import com.lianbang.staff.R
import com.lianbang.staff.adapter.ShopListAdapter
import com.lianbang.staff.bean.ShopDetailBean
import com.lianbang.staff.bean.ShopListBean
import com.lianbang.staff.databinding.FragmentShopBinding
import com.lianbang.staff.vm.MainVM
import com.lianbang.staff.vm.ShopVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.fragment_home.al_content
import kotlinx.android.synthetic.main.fragment_home.et_input
import kotlinx.android.synthetic.main.fragment_home.et_input2
import kotlinx.android.synthetic.main.fragment_home.fl_input
import kotlinx.android.synthetic.main.fragment_home.rv_list
import kotlinx.android.synthetic.main.fragment_shop.*
import kotlin.math.abs

/**
 * Created by maoqi on 2020/12/28.
 */
class ShopFragment : BaseListFragment<ShopVM, FragmentShopBinding, ShopListBean>()
    , OnLoadMoreListener,
    OnRefreshListener, OnItemClickListener {
    companion object {

        fun newInstance(): ShopFragment {
            return ShopFragment()
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_shop
    }

    override fun initData(arguments: Bundle?) {
        super.initData(arguments)
    }

    override fun initView(view: View) {
        super.initView(view)
        tv_shop_number.text = SpanUtils().append("45")
            .setForegroundColor(ResourcesUtils.getColor(R.color.white))
            .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_26))
            .setBold().append("家").create()
        tv_add.text = "21"
        et_input.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {

            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (et_input2.text.toString() != s.toString()) {
                    et_input2.setText(s)
                }
            }
        })
        et_input2.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (et_input.text.toString() != s.toString()) {
                    et_input2.setText(s)
                }
            }
        })
        al_content.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
            when {
                verticalOffset == 0 -> {
                    //完全展开

                }
                abs(verticalOffset) >= appBarLayout.totalScrollRange -> {
                    //完全折叠
                    if (fl_input.visibility != View.VISIBLE) {
                        fl_input.visibility = View.VISIBLE
                    }
                }
                else -> {
                    //过渡
                    if (fl_input.visibility != View.INVISIBLE) {
                        fl_input.visibility = View.INVISIBLE
                    }
                }
            }
        })
    }

    override fun loadData() {
        mViewModel.refreshData(ShopVM.TYPE_SHOP_LIST)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        val keyword = et_input.text.toString()
        mViewModel.refreshData(ShopVM.TYPE_SHOP_LIST, keyword)
    }

    override fun initAdapter(): BaseQuickAdapter<ShopListBean, *> {
        return ShopListAdapter()
    }

    override fun onLoadMore() {
        val keyword = et_input.text.toString()
        mViewModel.loadData(ShopVM.TYPE_SHOP_LIST, keyword)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val shopListBean = mListAdapter.data[position]
        ShopDetailActivity.start(activity, shopListBean.uid, shopListBean.nickname)
    }

}